package com.hyts.infodb.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;

import javax.servlet.http.HttpServletRequest;

/**
 * project-name:infodb
 * package-name:com.hyts.infodb.zuul.filter
 * author:Libo/Alex
 * create-date:2019-05-26 14:57
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于:
 */
public class AuthAccessFilter extends ZuulFilter {


    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        //获取token 的参数
        String token = request.getParameter("token");
        if(token == null) {
            //zuul不处理这条请求
            ctx.setSendZuulResponse(false);
            //设定返回的错误代码
            ctx.setResponseStatusCode(401);

            //写数据到浏览器
            ctx.setResponseBody("token is null");
            return null;
        }
        return null;
    }
}
